package com.boho.ecp.manage.order.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.POUtils;
import cn.boho.framework.po.Pager;
import cn.boho.framework.po.SArrayList;
import cn.boho.framework.po.UpdatePOList;
import cn.boho.framework.utils.DateUtils;

import com.boho.ecp.manage.member.dao.MemberDao;
import com.boho.ecp.manage.order.dao.TOrderDao;
import com.boho.ecp.po.TDeliverDetailPO;
import com.boho.ecp.po.TDeliverOrderPO;
import com.boho.ecp.po.TOrderChangeLogPO;
import com.boho.ecp.po.TOrderDetailPO;
import com.boho.ecp.po.TOrderPO;
import com.boho.ecp.util.JsonUtils;
import com.boho.ecp.util.SeqManager;

public class OrderService {

	/**
	 * 查询客户订单列表
	 * @param con
	 * @param pager
	 * @param orderId
	 * @param status
	 * @return
	 * @throws Exception
	 */
	public static ComboPager queryOrderByPage(Connection con, Pager pager, String orderId, Integer status) throws Exception{
		ComboPager cp = TOrderDao.queryTOrderByPager(con, pager, orderId, status);
		return cp;
	}
	
	/**
	 * 修改订单信息
	 * @param con
	 * @param orderId
	 * @param deliverMethod
	 * @param payMethod
	 * @param deliverTime
	 * @param consignee
	 * @param mobile
	 * @param telephone
	 * @param zipCode
	 * @param finalAddress
	 * @param productId
	 * @param prirce
	 * @param qty
	 * @throws Exception
	 */
	public static void updateOrderById(Connection con, String orderId, Integer deliverMethod, Integer payMethod, 
			Integer deliverTime, String consignee, String mobile, String telephone, String zipCode, String finalAddress,
			Integer[] productId, String[] prices, Integer[] qty, Integer operatorId) throws Exception{
		Date updateDate = new Date();
		
		//修改订单商品信息
		UpdatePOList updatePOList = new UpdatePOList();
		BigDecimal amount = new BigDecimal(0);
		for (int i = 0; i < productId.length; i++) {
			BigDecimal price = new BigDecimal(prices[i]);
			TOrderDetailPO c = new TOrderDetailPO();
			TOrderDetailPO v = new TOrderDetailPO();
			c.setOrderId(orderId);
			c.setProductId(productId[i]);
			v.setMemberPrice(price);
			
			if(qty[i] > 0){
				Integer point = price.multiply(new BigDecimal(qty[i])).toBigInteger().intValue();
				BigDecimal single = price.multiply(new BigDecimal(qty[i]));
				amount = amount.add(single);
				v.setQty(qty[i]);
				v.setPoint(point);
				v.setAmount(single);
//				v.setIsValid(Product.VAILD);
			} else {
				v.setQty(0);
				v.setPoint(0);
				v.setAmount(new BigDecimal(0));
//				v.setIsValid(Product.INVALID);
			}
			v.setUpdateBy(operatorId.toString());
			v.setUpdateDate(updateDate);
			updatePOList.add(c, v);
		}
		POUtils.updatePoList(con, updatePOList, false);
		
		//修改订单信息
		TOrderPO cPO = new TOrderPO();
		TOrderPO vPO = new TOrderPO();
		cPO.setOrderId(orderId);
		vPO.setDeliverMethod(deliverMethod);
		vPO.setPayMethod(payMethod);
		vPO.setDeliverTime(deliverTime);
		vPO.setConsignee(consignee);
		vPO.setMobile(mobile);
		vPO.setTelephone(telephone);
		vPO.setZipCode(zipCode);
		vPO.setFinalAddress(finalAddress);
		vPO.setPayment(amount);
		vPO.setOrderPoints(amount.toBigInteger().intValue());
		vPO.setUpdateBy(operatorId.toString());
		vPO.setUpdateDate(updateDate);
		POUtils.update(con, cPO, vPO, false);
	}
	
	/**
	 * 查询订单对象
	 * @param con
	 * @param orderId
	 * @param memberId
	 * @return
	 * @throws Exception
	 */
	public static TOrderPO getOrderPOById(Connection con, String orderId) throws Exception {
		TOrderPO tOrderPO = new TOrderPO();
		DynaBeanMap dynaBeanMap = getOrderById(con, orderId);
		if(dynaBeanMap!=null){
			tOrderPO.setOrderId(dynaBeanMap.get("ORDER_ID")==null?null:(String)dynaBeanMap.get("ORDER_ID"));
			tOrderPO.setProduct(dynaBeanMap.get("PRODUCT")==null?null:(String)dynaBeanMap.get("PRODUCT"));
			tOrderPO.setStatus(dynaBeanMap.get("STATUS")==null?null:(Integer)dynaBeanMap.get("STATUS"));
			tOrderPO.setMid(dynaBeanMap.get("MID")==null?null:(Integer)dynaBeanMap.get("MID"));
			tOrderPO.setConsignee(dynaBeanMap.get("PRODUCT")==null?null:(String)dynaBeanMap.get("PRODUCT"));
			tOrderPO.setMobile(dynaBeanMap.get("MOBILE")==null?null:(String)dynaBeanMap.get("MOBILE"));
			tOrderPO.setTelephone(dynaBeanMap.get("TELEPHONE")==null?null:(String)dynaBeanMap.get("TELEPHONE"));
			tOrderPO.setZipCode(dynaBeanMap.get("ZIP_CODE")==null?null:(String)dynaBeanMap.get("ZIP_CODE"));
			tOrderPO.setFinalAddress(dynaBeanMap.get("FINAL_ADDRESS")==null?null:(String)dynaBeanMap.get("FINAL_ADDRESS"));
			tOrderPO.setDeliverTime(dynaBeanMap.get("DELIVER_TIME")==null?null:(Integer)dynaBeanMap.get("DELIVER_TIME"));
			tOrderPO.setPayMethod(dynaBeanMap.get("PAY_METHOD")==null?null:(Integer)dynaBeanMap.get("PAY_METHOD"));
			tOrderPO.setDeliverMethod(dynaBeanMap.get("DELIVER_METHOD")==null?null:(Integer)dynaBeanMap.get("DELIVER_METHOD"));
			tOrderPO.setOrderPoints(dynaBeanMap.get("ORDER_POINTS")==null?null:(Integer)dynaBeanMap.get("ORDER_POINTS"));
			tOrderPO.setPayment(dynaBeanMap.get("PAYMENT")==null?null:(BigDecimal)dynaBeanMap.get("PAYMENT"));
			tOrderPO.setMemberId(dynaBeanMap.get("MEMBER_ID")==null?null:(Integer)dynaBeanMap.get("MEMBER_ID"));
			tOrderPO.setProcessUserId(dynaBeanMap.get("PROCESS_USER_ID")==null?null:(Integer)dynaBeanMap.get("PROCESS_USER_ID"));
			tOrderPO.setIsValid(dynaBeanMap.get("IS_VALID")==null?null:(Integer)dynaBeanMap.get("IS_VALID"));
			tOrderPO.setCreateBy(dynaBeanMap.get("CREATE_BY")==null?null:(String)dynaBeanMap.get("CREATE_BY"));
			tOrderPO.setCreateDate(DateUtils.TimestampToDate(dynaBeanMap.get("CREATE_DATE")));
			tOrderPO.setUpdateBy(dynaBeanMap.get("UPDATE_BY")==null?null:(String)dynaBeanMap.get("UPDATE_BY"));
			tOrderPO.setUpdateDate(DateUtils.TimestampToDate(dynaBeanMap.get("UPDATE_DATE")));
		}
		return tOrderPO;
	}
	
	/**
	 * 修改订单状态
	 * @param con
	 * @param orderId
	 * @param status
	 * @param operator
	 * @throws Exception
	 */
	public static void updateOrderStatus(Connection con, String orderId, int status, Date date, String operator) throws Exception {
		TOrderPO cPO = new TOrderPO();
		TOrderPO vPO = new TOrderPO();
		cPO.setOrderId(orderId);
		vPO.setStatus(status);
		vPO.setUpdateBy(operator);
		vPO.setUpdateDate(date);
		POUtils.update(con, cPO, vPO, false);
	}
	
	/**
	 * 修改订单处理信息
	 * @param con
	 * @param orderId
	 * @param date
	 * @param operator
	 * @param result
	 * @throws Exception
	 */
	public static void updateOrderResult(Connection con, String orderId, Date date, Integer operator, String result) throws Exception {
		TOrderPO cPO = new TOrderPO();
		TOrderPO vPO = new TOrderPO();
		cPO.setOrderId(orderId);
		vPO.setProcessUserId(operator);
		vPO.setProcessResult(result);
		vPO.setUpdateBy(operator.toString());
		vPO.setUpdateDate(date);
		POUtils.update(con, cPO, vPO, false);
	}
	
	/**
	 * 添加订单状态改变信息
	 * @param con
	 * @param orderId
	 * @param status
	 * @param date
	 * @param content
	 * @param operatorName
	 * @param createBy
	 * @throws Exception
	 */
	public static void addOrderChangeLog(Connection con, String orderId, int status, Date date, 
			String content, String operatorName, String createBy) throws Exception{
		TOrderChangeLogPO changeLogPO = new TOrderChangeLogPO();
		changeLogPO.setOrderId(orderId);
		changeLogPO.setStatus(status);
		changeLogPO.setChangeDate(date);
		changeLogPO.setContent(content);
		changeLogPO.setOperatorName(operatorName);
		changeLogPO.setCreateBy(createBy);
		changeLogPO.setCreateDate(date);
		POUtils.insert(con, changeLogPO, false);
	}
	
	/**
	 * 根据订单编号查询订单信息
	 * @param con
	 * @param orderId
	 * @param memberId
	 * @return
	 * @throws Exception
	 */
	public static DynaBeanMap getOrderById(Connection con, String orderId) throws Exception{
		DynaBeanMap dbm = TOrderDao.getTOrderByPk(con, orderId);
		return dbm;
	}
	
	/**
	 * 获取订单详细信息
	 * @param con
	 * @param orderId
	 * @param memberId
	 * @return
	 * @throws Exception
	 */
	public static DynaBeanMap getOrderDetailById(Connection con, String orderId) throws Exception {
		DynaBeanMap dbm = new DynaBeanMap();
		DynaBeanMap orderDbm = getOrderById(con, orderId);
		List detailList = TOrderDao.getOrderDetailById(con, orderId);
		List logList = TOrderDao.queryOrderLogList(con, orderId);
		Integer deliverId = (Integer)orderDbm.get("MID");
		DynaBeanMap delviverDbm = TOrderDao.getTDeliveryAddressByPk(con,deliverId);
		Integer memberId = (Integer)orderDbm.get("MEMBER_ID");
		DynaBeanMap member = MemberDao.getMemberByPk(con, memberId);
		dbm.put("ORDER", orderDbm);
		dbm.put("DETAIL", detailList.toArray());
		dbm.put("DETAIL_LIST", detailList);
		dbm.put("LOG", logList.toArray());
		dbm.put("ADDRESS", delviverDbm);
		dbm.put("MEMBER", member);
		return dbm;
	}
	
	/**
	 * 生成配送单
	 * @param con
	 * @param orderId
	 * @throws Exception
	 */
	public static void newDeliverOrder(Connection con, String orderId, Integer opertorId, String operator, Date operaDate) throws Exception{
		DynaBeanMap dbm = getOrderDetailById(con, orderId);
		SeqManager sm = SeqManager.getInstance();
		String id = sm.getSeq("yyMMddhhmmss","seq_deliver_order").toString();
		
		DynaBeanMap order = (DynaBeanMap)dbm.get("ORDER");
		List detail = (List)dbm.get("DETAIL_LIST");
		DynaBeanMap member = (DynaBeanMap)dbm.get("MEMBER");
		
		TDeliverOrderPO deliverOrderPO = new TDeliverOrderPO();
		deliverOrderPO.setId(id);
		deliverOrderPO.setOrderId(orderId);
		deliverOrderPO.setMemberAccount((String)member.get("MEMBER_ACCOUNT"));
		deliverOrderPO.setMemberName((String)order.get("CONSIGNEE"));
		deliverOrderPO.setPhone(StringUtils.isEmpty((String)order.get("MOBILE")) == true ? (String)order.get("TELEPHONE") : (String)order.get("MOBILE"));
		deliverOrderPO.setAddress((String)order.get("FINAL_ADDRESS"));
		deliverOrderPO.setDeliverTime((Integer)order.get("DELIVER_TIME"));
		deliverOrderPO.setDeliverMethod((Integer)order.get("DELIVER_METHOD"));
		deliverOrderPO.setRemark((String)order.get("MESSAGE"));
		deliverOrderPO.setPayment((BigDecimal)order.get("PAYMENT"));
//		deliverOrderPO.setPrintDate(operaDate);
//		deliverOrderPO.setOperator(operator);
		deliverOrderPO.setCreateBy(opertorId.toString());
		deliverOrderPO.setCreateDate(operaDate);
		POUtils.insert(con, deliverOrderPO, false);
		
		SArrayList poList = new SArrayList();
		for (Object object : detail) {
			DynaBeanMap m = (DynaBeanMap)object;
			TDeliverDetailPO deliverDetailPO = new TDeliverDetailPO();
			deliverDetailPO.setDeliverOrderId(id);
			deliverDetailPO.setOrderId(orderId);
			deliverDetailPO.setProductId((Integer)m.get("PRODUCT_ID"));
			deliverDetailPO.setProductNo((String)m.get("PRODUCT_NO"));
			deliverDetailPO.setProductName((String)m.get("PRODUCT_NAME"));
			deliverDetailPO.setMemberPrice((BigDecimal)m.get("MEMBER_PRICE"));
			deliverDetailPO.setQty((Integer)m.get("QTY"));
			deliverDetailPO.setAmount((BigDecimal)m.get("AMOUNT"));
			deliverDetailPO.setMemberId((Integer)m.get("MEMBER_ID"));
			deliverDetailPO.setOrderDate((Date)m.get("ORDER_DATE"));
			deliverDetailPO.setCreateBy(opertorId.toString());
			deliverDetailPO.setCreateDate(operaDate);
			poList.add(deliverDetailPO);
		}
		POUtils.insertPoList(con, poList, false);
	}
	
	/**
	 * 查询配送单信息
	 * @param con
	 * @param orderId
	 * @return
	 * @throws Exception
	 */
	public static DynaBeanMap getDeliverOrder(Connection con, String orderId) throws Exception{
		DynaBeanMap dbm = TOrderDao.getDeliverOrder(con, orderId);
		List list = TOrderDao.queryDeliverDetailList(con, orderId);
		if(dbm != null){
			dbm.put("PRODUCT_LIST", JsonUtils.toJSON(list));
		}
		return dbm;
	}
	
	/**
	 * 更新打印日期
	 * @param con
	 * @param operator
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static void updatePrintDate(Connection con, String id, String operator, Date date) throws Exception{
		TDeliverOrderPO cPO = new TDeliverOrderPO();
		TDeliverOrderPO vPO = new TDeliverOrderPO();
		cPO.setId(id);
		vPO.setPrintDate(date);
		vPO.setOperator(operator);
		POUtils.update(con, cPO, vPO, false);
	}
	
}
